#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>


int bin_search(int arr[], int left, int right, int key)
{
	while (left < right)
	{
		if (arr[left] == key)
		{
			return left;
		}
		if (arr[right] == key)
		{
			return right;
		}
		int mid = (left + right) / 2;
		if (arr[mid] > key)
		{
			right = mid - 1;
		}
		else
		{
			if (arr[mid] < key)
			{
				left = mid + 1;
			}
			else
			{
				return mid;
			}
		}
	}
	return 0;
}

int main()
{
	int a[] = {0,12,22,31,47,87,99};
	//         0  1  2  3  4  5  6
	printf("%d", bin_search(a, 0, 6, 50));
	return 0;
}